package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.AuthAdminContent;
import com.xmy.cultivate.entity.LackCourseLog;
import com.xmy.cultivate.entity.TrackMarket;
import com.xmy.cultivate.entity.response.LackRecourseRate;
import com.xmy.cultivate.entity.views.LeadDataRs;
import com.xmy.cultivate.mapper.LackCourseLogMapper;
import com.xmy.cultivate.mapper.TrackMarketMapper;
import com.xmy.cultivate.service.ILackCourseLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.ITrackMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 缺课记录 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-07-28
 */
@Service
public class LackCourseLogServiceImpl extends ServiceImpl<LackCourseLogMapper, LackCourseLog> implements ILackCourseLogService {


    @Autowired
    LackCourseLogMapper lackCourseLogMapper;

    @Autowired
    TrackMarketMapper trackMarketMapper;

    @Autowired
    ITrackMarketService iTrackMarketService;

    @Override
    public List<LackRecourseRate> getLackRade(String schoolId, Long subjectsId, Integer type, Integer yearPart, Integer quarterNum, String startDate, String endDate, Long trackId, String lessonType) {
        List<LackRecourseRate> lackRecourseRateList = lackCourseLogMapper.getLackRade(yearPart, quarterNum, schoolId, subjectsId, type, startDate, endDate, trackId, lessonType);
        return lackRecourseRateList;
    }

    public IPage<LackRecourseRate> getLackRadeDetail(IPage<?> iPage, Integer yearPart, Integer quarterNum, Long schoolId, Long teacherId, Long gradeId, Long subjectsId, Integer type, String startDate, String endDate, Long trackId, Integer detailType, String lessonType, String isLack, String stuType) {
        return lackCourseLogMapper.getLackRadeDetail(iPage, yearPart, quarterNum, schoolId, teacherId, gradeId, subjectsId, type, startDate, endDate, trackId, detailType, lessonType, isLack, stuType);
    }

    @Override
    public List<LackRecourseRate> getRepairLackRade(String schoolId, String subjectsId, Integer type, Integer yearPart, Integer quarterNum, String startDate, String endDate, Long trackId, String lessonType, String yearClassId, String classTypeIdStr, String startLessonNum, String endLessonNum, String lackType, AuthAdminContent authAdminContent, LeadDataRs leadDataRs) {
        List<LackRecourseRate> lackRecourseRateList = lackCourseLogMapper.getRepairLackRade(yearPart, quarterNum, schoolId, subjectsId, type, startDate, endDate, trackId, lessonType, yearClassId, classTypeIdStr, startLessonNum, endLessonNum, lackType, authAdminContent, leadDataRs);
        return lackRecourseRateList;
    }

    @Override
    public IPage<LackRecourseRate> getRepairLackRadeDetail(IPage<?> iPage, String schoolId, Long teacherId, Long gradeId, String subjectsId, Integer type, Integer yearPart, Integer quarterNum, String startDate, String endDate, Long trackId, String lessonType, String yearClassId, String isRepair, String classTypeIdStr, Integer isCollect, String lessonNum, String startLessonNum, String endLessonNum, String lackType, AuthAdminContent authAdminContent, LeadDataRs leadDataRs) {
        return lackCourseLogMapper.getRepairLackRadeDetail(iPage, yearPart, quarterNum, schoolId, teacherId, gradeId, subjectsId, type, startDate, endDate, trackId, lessonType, yearClassId, isRepair, classTypeIdStr, isCollect, lessonNum, startLessonNum, endLessonNum, lackType, authAdminContent, leadDataRs);
    }

    @Override
    public List<LackRecourseRate> getRepairLackRadeTeamList(Integer yearPart, Integer quarterNum, String subjectsId, String startDate, String endDate, String yearClassId, String classTypeIdStr, String teamId, String classify, String trackType, String startLessonNum, String endLessonNum, String lackType, AuthAdminContent authAdminContent, String schoolId, LeadDataRs leadDataRs) {
        StringBuilder ids = new StringBuilder();
        TrackMarket market = trackMarketMapper.selectById(teamId);
        if (Objects.nonNull(teamId) && Objects.nonNull(market)) {
            ids.append(teamId);
            if (market.getPid().equals(0) || market.getPid() == 0L) {
                String idByPid = trackMarketMapper.getIdByPid(market.getId(), String.valueOf(yearPart), String.valueOf(quarterNum), Integer.valueOf(classify));
                if (StringUtils.isNotBlank(idByPid)) {
                    ids.append(",");
                    ids.append(idByPid);
                }
            }
        }
        List<LackRecourseRate> lackRecourseRateList = lackCourseLogMapper.getRepairLackRadeTeamList(yearPart, quarterNum, subjectsId, startDate, endDate, yearClassId, classTypeIdStr, teamId, classify, trackType, startLessonNum, endLessonNum, lackType, authAdminContent, schoolId, leadDataRs);

        return lackRecourseRateList;
    }

    @Override
    public IPage<LackRecourseRate> getRepairLackRadeTeamDetail(IPage<?> page, Integer yearPart, Integer quarterNum, String schoolId, Long teacherId, Long gradeId, String subjectsId, String startDate, String endDate, String yearClassId, String isRepair, String classTypeIdStr, String teamId, String classify, String trackType, Integer isCollect, String lessonNum, String startLessonNum, String endLessonNum, String lackType, AuthAdminContent authAdminContent, LeadDataRs leadDataRs) {
        StringBuilder ids = new StringBuilder();
        TrackMarket market = trackMarketMapper.selectById(teamId);
        if (Objects.nonNull(teamId) && Objects.nonNull(market)) {
            ids.append(teamId);
            if (market.getPid().equals(0) || market.getPid() == 0L) {
                String idByPid = trackMarketMapper.getIdByPid(market.getId(), String.valueOf(yearPart), String.valueOf(quarterNum), Integer.valueOf(classify));
                if (StringUtils.isNotBlank(idByPid)) {
                    ids.append(",");
                    ids.append(idByPid);
                }
            }
        }

        IPage<LackRecourseRate> lackRecourseRateIPage = lackCourseLogMapper.getRepairLackRadeTeamDetail(page, yearPart, quarterNum, schoolId, teacherId, gradeId, subjectsId, startDate, endDate, yearClassId, isRepair, classTypeIdStr, teamId, classify, trackType, isCollect, lessonNum, startLessonNum, endLessonNum, lackType, authAdminContent, leadDataRs);

        return lackRecourseRateIPage;
    }
}
